package cn.com.yusys.yusp.admin.service;

import cn.com.yusys.yusp.admin.domain.AdminSmResContr;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmAuthRecoMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmDataAuthMapper;
import cn.com.yusys.yusp.admin.repository.mapper.AdminSmResContrMapper;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.security.SecurityUtils;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.commons.util.DateUtil;

import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @项目名称: yusp-admin
 * @类名称: AdminContrPointService
 * @类描述: 控制点管理服务层
 * @功能描述:
 * @创建人: weimei1@yusys.com.cn
 * @创建时间: 2017-12-21 17:57
 * @修改备注:
 * @修改记录: 修改时间 修改人员 修改原因
 *        -------------------------------------------------------------
 * @version 1.0.0
 * @Copyright (c) 2017宇信科技-版权所有
 */
@Service
public class AdminSmResContrService extends CommonService {

    @Autowired
    private AdminSmResContrMapper adminSmResContrMapper;

    @Autowired
    private AdminSmDataAuthMapper adminSmDataAuthMapper;

    @Autowired
    private AdminSmAuthRecoMapper adminSmAuthRecoMapper;
    @Override
    protected CommonMapper<?> getMapper() {
        return this.adminSmResContrMapper;
    }

    /**
     * @方法名称:getContrInfo
     * @方法描述:查询控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getContrInfo(QueryModel model) {
        PageHelper.startPage(model.getPage(), model.getSize());
        if (model.getCondition().containsKey("contrCode")) {
            model.getCondition().put("contrCode", "%" + model.getCondition().get("contrCode") + "%");
        }
        if (model.getCondition().containsKey("contrName")) {
            model.getCondition().put("contrName", "%" + model.getCondition().get("contrName") + "%");
        }
        if (model.getCondition().containsKey("contrUrl")) {
            if(model.getCondition().get("contrUrl").equals("")){
                model.getCondition().put("contrUrl",null);
            }else {
                model.getCondition().put("contrUrl", "%" + model.getCondition().get("contrUrl") + "%");
            }
        }
        List<Map<String, Object>> list = this.adminSmResContrMapper.getContrInfo(model);
        PageHelper.clearPage();
        return list;
    }

    /**
     * @方法名称: ifCodeRepeat
     * @方法描述: 保存控制点信息前，判断业务功能是否已关联相同控制操作代码的控制点
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> ifCodeRepeat(String funcId, String contrCode, String contrId) {
        Map<String, Object> params = new HashMap<>();
        params.put("funcId", funcId);
        params.put("contrCode", contrCode);
        params.put("contrId", contrId);
        return adminSmResContrMapper.ifCodeRepeat(params);
    }

    /**
     * @方法名称: createContr
     * @方法描述: 新增控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int createContr(AdminSmResContr adminSmResContr) {
    	adminSmResContr.setLastChgDt(DateUtil.formatDateTimeByDef());   
    	if(null == adminSmResContr.getLastChgUsr()||"".equals(adminSmResContr.getLastChgUsr())) { 
    		return 0; 
    	}else { 
    		int r=this.adminSmResContrMapper.insertSelective(adminSmResContr); 
    		if(r==1) { 
    			return 1; 
    		}else { 
    			return 0; 
    		} 
    	 } 

    }

    /**
     * @方法名称: editContr
     * @方法描述: 修改控制点信息
     * @参数与返回说明:
     * @算法描述:
     */
    public int editContr(AdminSmResContr adminSmResContr) {
    	adminSmResContr.setLastChgUsr(SecurityUtils.getCurrentUserLogin());
        adminSmResContr.setLastChgDt(DateUtil.formatDateTimeByDef());
        return this.updateSelective(adminSmResContrMapper, adminSmResContr);
    }

    /**
     * @方法名称: deleteContr
     * @方法描述: 删除控制点时删除对应的数据权限及二者的授权数据
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = false, rollbackFor = { Exception.class, RuntimeException.class })
    public int deleteContr(String ids) {
        int n = 0;
        if (ids != null && !"".equals(ids)) {
            String[] contrIds = ids.split(",");
            List<String> list = this.adminSmResContrMapper.getDataByContrId(contrIds);
            if (list.size() > 0) {
                String[] authId = new String[list.size()];
                for (int j = 0, listLen = list.size(); j < listLen; j++) {
                    authId[j] = list.get(j).toString();
                    this.adminSmDataAuthMapper.deleteByPrimaryKey(list.get(j));
                }
                this.adminSmAuthRecoMapper.deleteDataAuth(authId);
            }
            this.adminSmAuthRecoMapper.deleteContrInfo(contrIds);
            for (int i = 0; i < contrIds.length; i++) {
                if (!"".equals(contrIds[i])) {
                    n = n + this.adminSmResContrMapper.deleteByPrimaryKey(contrIds[i]);
                }
            }
        }
        return n;
    }

    /**
     * @方法名称: getFuncTree
     * @方法描述: 左侧业务功能树查询
     * @参数与返回说明:
     * @算法描述:
     */
    @Transactional(readOnly = true)
    public List<Map<String, Object>> getFuncTree(QueryModel model) {
        List<Map<String, Object>> list = adminSmResContrMapper.getFuncTree(model);
        return list;
    }
}
